const app = getApp();
const url = app.globalData.url;
const url1 = app.globalData.url1;
const datautil1 = require("../../utils/data.js");
const datautil2 = require("../../utils/util.js");
var data1 = "" //师傅日期
var data2 = '' //个人时间
var data3 = '' // 师傅时间
var data4 = '' //个人日期
var timeType = 0 //当前时间为0 预约为1
var menuFaultId = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    names: "维修师选择",
    flag: true,
    hours1: [{ time: "上午", timeframe: "08:00~10:00" }, { time: "中午", timeframe: "11:00~14:00" }, { time: "下午", timeframe: "14:00~17:00" }, { time: "晚上", timeframe: "17:00~20:00" }],
    ttIndex: 0,
    tabIndex: 0,
    engineers: [1,2,2],
    type: false,
    times: [],
    hours: [],
    date1Index: 0,
    rankitem:0
  },
  back() {
    wx.navigateBack(1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this
    that.getDates()
    console.log(options)
    menuFaultId = options.menuId
    var date = datautil2.formatTime(new Date(), false)
    that.getMytime(date)
  },
  //根据维修师时间
  getEnlist(mid) {
    const that = this
    wx.request({
      url: url1 + '/menu/fault/engineer/find',
      data: {
        menuFaultId: mid
      },
      method: "get",
      header: {
        "content-type": "application/json"
      },
      success(res) {

        console.log(res.data)
        if (res.data.code === 1) {
          that.setData({
            engineers: res.data.data.engineerList
          })
        } else {
          //无数据
          that.setData({
            engineers: []
          })
        }
      }
    })
  },

  //  根据我的时间
  getMytime(data) {
    const that = this
    wx.request({
      url: url1+ '/engineer/selectEngineerByUserTime',
      data: {
        repairType: menuFaultId,
        userTime: data
      },
      method: "get",
      header: {
        "content-type": "application/json"
      },
      success(res) {
        
        console.log(res.data)
        if (res.data.code === 1) {
          that.setData({
            engineers: res.data.data.engineer
          })
        } else {
          //无数据
          that.setData({
            engineers: []
          })
        }
      }
    })
  },


  choseterm(e) {
    const that = this

    that.setData({
      tabIndex: e.currentTarget.id
    })
    if (e.currentTarget.id == 0) {
      that.getMytime(e.currentTarget.dataset.date)
    } else {
      that.getEnlist(menuFaultId)
     
    }
  },

  // 根据维修师选择时间
  chooseTime(e) {
    const that = this
    console.log(e)
    that.showPopup()
    wx.setStorageSync("engneer", e.currentTarget.dataset.item)
    that.setData({
      type: false
    })
    wx.request({
      url: url1+'/GetEngineerTime/TimeFrame/selectEngineerTime',
      data: {
        EngineerNumber: e.currentTarget.id
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res.data)
      //   if(res.data.code===1){
            that.data.hours = res.data
            data1 = res.data[0].date
            that.setData({
              times: res.data[0].timeFrames
            })
        
        // }
      }
    })
  },
  selectbyrank(){
    this.setData({
      rankitem:0
    })
   },
  selectbyrank1() {
    this.setData({
      rankitem: 1
    })
   },
  selectbyrank2() { 
    this.setData({
      rankitem: 2
    })
  },
  // 根据个人时间
  configone(e) {
    const that = this
    var tt=that.data.ttIndex
    if(tt==0){
      wx.setStorageSync('time', datautil2.formatTime(new Date(), false))
    }else{
      wx.setStorageSync('time', data2+data4)
    }
    wx.setStorageSync("engneer", e.currentTarget.dataset.item)
    wx.navigateBack(1);
    that.showPopup()
  },
  selecttime() {
    const that = this
    this.showPopup()
    that.setData({
      type: false
    })

  },

  showPopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.show();
  },
  hidePopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    popupComponent && popupComponent.toggle(false);
    // popupComponent && popupComponent.hide();
  },

  popupTap() {
    let popupComponent = this.selectComponent('.J_Popup');

    popupComponent && popupComponent.toggle(false);
  },
  // 弹出框

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  // 选择日期
  selectDate(e) {
    const that = this
    console.log(e)
    var index = e.currentTarget.dataset.index
    //var time=that.data.hours
    console.log("that.data.times: ")
    console.log(e.currentTarget.dataset.dates)

    data2 = e.currentTarget.dataset.dates
    that.setData({
      dateIndex: index,
      date: data2,
      datess:data2
      // times: time[index].timeFrames
    })

  },
  // 选择时间
  selectTime(e) {
    const that = this
    console.log(e)
    data4 = e.currentTarget.dataset.time
    that.setData({
      timeIndex: e.currentTarget.dataset.index,
      time: data4,
      timess:data4
    })
  },
  selectDate1(e) {
    const that = this
    console.log(e)
    var index = e.currentTarget.dataset.index
    data1 = e.currentTarget.dataset.dates
    that.setData({
      date1Index: index,
      datess: data1,
    })
  },
  selectTime1(e) {
    const that = this
    console.log(e)
    data3 = e.currentTarget.dataset.time
    that.setData({
      time1Index: e.currentTarget.dataset.index,
      timess:data3
    })
  },
  getDates() {
    const that = this
    var time = datautil2.formatTime(new Date(), true);

    //获取一周的时间戳
    var toda = datautil1.getDates(5, time)
    console.log(toda)
    that.setData({
      datas: toda,
      currentDate: datautil2.formatTime(new Date(), false)
    })
  },
  // 确认个人的时间
  configTime() {
    const that = this
    if (data2.length == 0 || data4.length == 0) {
      wx.showToast({
        title: '选择日期时间',
        icon: "loading",
        duration: 1000
      })
    } else {
      that.getMytime(data2 + data4)
      this.hidePopup()

    }
  that.setData({
    selectconfig:true
  })

  },
  configTime1() {
    console.log("data3时间：")
    console.log(data3.length)
    if (data3.length == 0||data1.length==0) {
      wx.showToast({
        title: '请选择时间',
        icon: "loading",
        duration: 1000
      })
    } else {
      wx.setStorageSync('time', data1 +  data3)
      wx.navigateBack(1)
    }

  },

  selections(data) {
    console.log(data)
    const that = this
    if (data.currentTarget.dataset.index == 0) {
      timeType = 0
      that.setData({
        select: false,
        ttIndex: data.currentTarget.dataset.index
      })
    } else {
      timeType = 1
      this.showPopup()
      that.setData({
        select: true,
        ttIndex: data.currentTarget.dataset.index,
        type:true
      })
    }

  },

  /** 
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})